const paths = require('./paths')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const app = [
  {
    key: 'article',
    indexHtml: 'src/article/article.html',
    IndexJs: 'src/article/index',
  },
  {
    key: 'admin',
    indexHtml: 'src/admin/admin.html',
    IndexJs: 'src/admin/index',
  }
]

function getEntry (app) {
  let entry = {}
  app.forEach(item => {
    entry[item.key] = paths.resolveModule(paths.resolveApp, item.IndexJs)
  })
  return entry
}

function getPlugins (app) {
  let blogPlugin = []
  app.forEach(item => {
    let config = {
      inject: true,
      chunks: [item.key],
      template: paths.resolveApp(item.indexHtml),
      filename: item.key + '.html'
    }
    if ('production' === process.env.NODE_ENV) {
      config.minify = {
        removeComments: true,
        collapseWhitespace: true,
        removeRedundantAttributes: true,
        useShortDoctype: true,
        removeEmptyAttributes: true,
        removeStyleLinkTypeAttributes: true,
        keepClosingSlash: true,
        minifyJS: true,
        minifyCSS: true,
        minifyURLs: true,
      }
    }
    blogPlugin.push(new HtmlWebpackPlugin(config))
  })
  return blogPlugin
}

//修改buildjs和startjs
function getResolvePage (app) {
  let resolvePages = []
  app.forEach(item => {
    resolvePages.push(
      paths.resolveApp(item.indexHtml),
      paths.resolveModule(paths.resolveApp, item.IndexJs)
    )
  })
  return resolvePages
}

module.exports = {
  entry: getEntry(app),
  blogPlugin: getPlugins(app),
  resolvePage: getResolvePage(app)
}
